﻿Imports System.Net

Public Class frmPremium
    Dim key As String
    ' TODO: Insert code to perform custom authentication using the provided username and password 
    ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
    ' The custom principal can then be attached to the current thread's principal as follows: 
    '     My.User.CurrentPrincipal = CustomPrincipal
    ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
    ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
    ' such as the username, display name, etc.

    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        key = tbKey.Text
        If runAuth(key) = True Then
            MessageBox.Show("Auth Success")
        Else
            MessageBox.Show("Auth Fail")
        End If
    End Sub

    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub
    Function runAuth(ByVal fkey As String) As String
        Dim client As New WebClient()
        Dim verUrl As New Uri("https://meamod.com/secure/download/meamodplayme/prem/?key=" & fkey & "&hash=" & getMD5Hash(fkey))
        If verUrl.IsLoopback = False And verUrl.IsUnc = False And verUrl.Authority = "meamod.com" Then
            Dim returnval As String = client.DownloadString(verUrl)
            If returnval = getMD5Hash(fkey) Then
                Return True
            End If
        End If
        Return False
    End Function


    Function getMD5Hash(ByVal strToHash As String) As String
        Dim md5Obj As New System.Security.Cryptography.MD5CryptoServiceProvider
        Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)

        bytesToHash = md5Obj.ComputeHash(bytesToHash)
        Dim strResult As String = ""
        For Each b As Byte In bytesToHash
            strResult += b.ToString("x2")
        Next
        Return strResult
    End Function

    Private Sub frmPremium_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub
End Class
